<script setup>
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'

	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import {
		getUserProfile
	} from '@/api/system/user'
	import {
		addAdministrativeApply
	} from '@/api/attendance'
	import approvalProcessState from '@/store/modules/approvalProcess'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'

	const title = '合同流转'

	const entertainDateRef = ref(null)
	const contractCirculationList = ref({
		contractName: '',
		opposingUnitsName: '',
		signingDate: '',
		ourUnitsName: '',
		ourUnitsHead: '',
		ourUnitsHeadId: '',
		contractAmount: '',
	})

	const formatter = (type, value) => {
		if (type === 'year') {
			return `${value}年`
		}
		if (type === 'month') {
			return `${value}月`
		}
		if (type === 'day') {
			return `${value}日`
		}
		if (type === 'hour') {
			return `${value}时`
		}
		if (type === 'minute') {
			return `${value}分`
		}
		return value
	}

	const signingDateRef = ref(null)
	const signingDatePicker = ref(null)
	const signingDateFn = () => {
		signingDateRef.value.open()
	}
	const signingDateConfirm = (obj) => {
		contractCirculationList.value.signingDate = dayjs(obj.value).format('YYYY-MM-DD')
	}

	const userId = ref(null)
	const deptId = ref(null)
	const getUserProfileFn = async () => {
		let {
			code,
			data
		} = await getUserProfile()
		if (code === 200) {
			deptId.value = data.deptId
			userId.value = data.userId
		}
	}

	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=合同流转&apiName=行政',
		})
	}
	let attchment = ref([])
	const getDesignDrawingsEmit = (emit) => {
		attchment.value = emit
	}
	const clickSave = async () => {
		if (isObjectValuesNotEmpty(contractCirculationList.value)) {
			let params = {
				name: contractCirculationList.value.contractName,
				otherCompanyName: contractCirculationList.value.opposingUnitsName,
				date: dayjs(contractCirculationList.value.signingDate).format('YYYY-MM-DD hh:mm:ss'),
				ourUnitsName: contractCirculationList.value.ourUnitsName,
				functionary: contractCirculationList.value.ourUnitsHeadId,
				userId: userId.value,
				deptId: deptId.value,
				money: contractCirculationList.value.contractAmount,
				ruleId: 'hetongliuzhuan',
				type: '合同流转',
				attchment: JSON.stringify(attchment.value),
				approve: approvalProcessState.state.approvalProcess
			}
			let {
				code
			} = await addAdministrativeApply(params)
			if (code === 200) {
				uni.showToast({
					title: '提交成功',
					duration: 1500,
					icon: 'success',
				})

				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}

	}
	const usersFn = () => {
		uni.navigateTo({
			url: '/components/OptionDeptPerson/index?type=person&isDanXuan=1',
		})
	}
	onShow(() => {
		// if (optionDeptPerson.state.optionPerson) {
		//   contractCirculationList.value.ourUnitsHead = optionDeptPerson.state.optionPerson[0].nickName
		//   contractCirculationList.value.ourUnitsHeadId = optionDeptPerson.state.optionPerson[0].userId
		// }
		// 监听事件 获取选择的人员
		uni.$on('getOptionPerson', (data) => {
			console.log(data, '人员data')
			contractCirculationList.value.ourUnitsHead = data[0].nickName
			contractCirculationList.value.ourUnitsHeadId = data[0].userId
		});
	})

	onLoad(() => {
		getUserProfileFn()
	})
</script>

<template>
	<view>
		<!--    合同流转  -->
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
		</HeaderNavTabarSvg>

		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">合同名称</text>
				<view class="info-list-right">
					<view class="info-content">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="contractCirculationList.contractName" />
					</view>
				</view>
			</view>
		</view>
		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">对方单位名称</text>
				<view class="info-list-right">
					<view class="info-content">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="contractCirculationList.opposingUnitsName" />
					</view>
				</view>
			</view>
		</view>

		<view class="picker-content" @tap="signingDateFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">签约日期</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="contractCirculationList.signingDate">{{ contractCirculationList.signingDate }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="signingDateRef" v-model="signingDatePicker" :formatter="formatter" mode="date"
			@confirm="signingDateConfirm" />

		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">我方单位名称</text>
				<view class="info-list-right">
					<view class="info-content">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="contractCirculationList.ourUnitsName" />
					</view>
				</view>
			</view>
		</view>
		<view class="picker-content" @tap="usersFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">我方负责人</text>
				<view class="info-list-right">
					<view class="info-content">
						<view class="uni-input" v-if="contractCirculationList.ourUnitsHead">
							{{ contractCirculationList.ourUnitsHead }}
						</view>
						<view class="uni-inputs" v-else style="font-size: 28rpx">请选择</view>
					</view>
				</view>
			</view>
		</view>

		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">合同金额</text>
				<view class="info-list-right">
					<view class="info-content" style="display: flex; align-items: center; ">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="contractCirculationList.contractAmount" />
						<p style="font-size: 28rpx;line-height: 1.4em;">元</p>
					</view>
				</view>
			</view>
		</view>

		<!--    说明附件-->
		<DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" />
		<!--    审批流程-->
		<AuditApplicationSteps :ruleId="'hetongliuzhuan'" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.uni-input {
		text-align: right;
		font-size: 28rpx;
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.card-list {
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.list {
				box-shadow: 0 4rpx 16rpx 0 #0000000d;
				box-sizing: border-box;
				width: 100%;
				background: #fff;
				padding: 16rpx 24rpx;
				border-radius: 8rpx;
				margin-bottom: 10rpx;

				h4 {
					color: #1d2129;
					font-size: 32rpx;
					font-weight: 400;
				}

				p {
					color: #86909c;
					font-size: 24rpx;
					font-weight: 400;
					margin-top: 10rpx;
				}
			}
		}
	}

	.detail-itemRequisition {
		.detail-item {
			padding: 10rpx 30rpx;
			@include flex($align: center, $justify: space-between);

			.detail-utils {
				@include flex($align: center, $justify: space-between);
				width: 150rpx;
				color: #165dff;
			}
		}

		.text-info {
			color: #86909c;
			padding: 20rpx 30rpx;
			display: inline-block;
		}
	}

	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}
</style>